package org.nightstudio.common.face.action.spi;

import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.nightstudio.common.bean.Pagination;
import org.nightstudio.common.util.action.ActionUtil;
import org.nightstudio.common.util.constant.ApplicationConstant;
import org.nightstudio.common.util.constant.RequestConstant;
import org.nightstudio.common.util.error.ErrorUtil;
import org.nightstudio.common.util.exception.sys.NightStudioException;
import org.nightstudio.common.util.exception.sys.SystemInternalException;
import org.nightstudio.common.util.session.SessionUtil;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

public class NightStudioAction extends SimpleFormController {
	private static Logger logger = Logger.getLogger(NightStudioAction.class);
	
	@Override
	public ModelAndView handleRequest(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		return super.handleRequest(request, response);
	}

	protected ModelAndView getSuccessModelAndView(HttpServletRequest request, Map<String, Object> model) {
		if (request.getAttribute(RequestConstant.NULL_SUCCESS_VIEW) != null && request.getAttribute(RequestConstant.NULL_SUCCESS_VIEW).equals(this.getClass())) {
			request.removeAttribute(RequestConstant.NULL_SUCCESS_VIEW);
			return null;
		}
		return getModelAndView(this.getSuccessView(), model);
	}

	private ModelAndView getModelAndView(String view, Map<String, Object> model) {
		return new ModelAndView(view, model);
	}
	
	protected void redirect(HttpServletRequest request, HttpServletResponse response, String path) throws Exception {
		request.setAttribute(RequestConstant.NULL_SUCCESS_VIEW, this.getClass());
		response.sendRedirect(path);
	}
	
	protected void forward(HttpServletRequest request,
			HttpServletResponse response, String path) throws Exception {
		this.forward(request, response, path, null);
	}
	
	protected void forward(HttpServletRequest request,
			HttpServletResponse response, String path, Map<String, Object> model) throws Exception {
		if (model != null) {
			for (String key : model.keySet()) {
				request.setAttribute(key, model.get(key));
			}
		}
		request.setAttribute(RequestConstant.NULL_SUCCESS_VIEW, this.getClass());
		if (path.startsWith(ActionUtil.getContextPath(request))) {
			path = path.substring(ActionUtil.getContextPath(request).length());
		}
		request.getRequestDispatcher(path).forward(request, response);
	}
	
	protected ModelAndView handleThrowable(HttpServletRequest request,
			HttpServletResponse response, Throwable t) throws Exception {
		logger.warn("MimsAction handle throwable: ", t);
		if (t instanceof NightStudioException) {
			return ErrorUtil.getErrorView((NightStudioException) t);
		}
		return ErrorUtil.getErrorView(new SystemInternalException());
	}
	
	protected Pagination getPagination(HttpServletRequest request) {
		return this.getPagination(request, ApplicationConstant.PAGE_SIZE);
	}
	
	protected Pagination getPagination(HttpServletRequest request, int pageSize) {
		String strCurrPage = request.getParameter(RequestConstant.PARAM_KEY_CURRENT_PAGE);
		Pagination pagination;
		if (strCurrPage != null
			&& !"".equals(strCurrPage.trim())
			&& (pagination = SessionUtil.getPagination(request)) != null
			&& pagination.isInit()) {
			long currPage = Long.parseLong(strCurrPage);
			pagination.setCurrPage(currPage);
		} else {
			pagination = new Pagination(pageSize);
		}
		SessionUtil.setPagination(request, pagination);
		return pagination;
	}
	
	protected void transformParameters(HttpServletRequest request) {
		@SuppressWarnings("unchecked")
		Set<String> keys = request.getParameterMap().keySet();
		for (String key : keys) {
			if (request.getParameterValues(key) != null) {
				if (request.getParameterValues(key).length == 1) {
					request.setAttribute(key, request.getParameter(key));
				} else {
					request.setAttribute(key, request.getParameterValues(key));
				}
			}
		}
	}
	
	protected void transformParameters(HttpServletRequest request, Map<String, Object> model) {
		@SuppressWarnings("unchecked")
		Set<String> keys = request.getParameterMap().keySet();
		for (String key : keys) {
			if (request.getParameterValues(key) != null) {
				if (request.getParameterValues(key).length == 1) {
					model.put(key, request.getParameter(key));
				} else {
					model.put(key, request.getParameterValues(key));
				}
			}
		}
	}
}
